window.CKEDITOR_BASEPATH = '<?=_EDITOR_URL;?>';

$(document).ready(function() {
	
	$("#MySplitter").splitter({
		type: "v",
		minLeft: 0, sizeLeft: 246, maxLeft: 600,
		anchorToWindow: true,
		cookie: "spltr",
		cookiePath: '<?php echo MozgRoute::Get('http')->Create(array('module' => 'admin')); ?>',
		resizeToWidth: true,
		outline: true
	});
	$('#LeftPane, #RightPane').css('visibility', 'visible');
	
	var loadStatus = $('.loading');

	$.ajaxSetup({
	  timeout: 30000,
	  cache: true,
	  beforeSend: function (event){
			loadStatus.fadeIn();
	  },
	  complete: function (event, textStatus) {
			loadStatus.fadeOut();
	  },
	  error: function (XMLHttpRequest, textStatus, errorThrown){
		  showMessage('Неизвестная ошибка', true, true);
	  }
	});
	//-----------------------	

	//-------------------
	$('a.del').livequery('click', function(){
		return confirmDelElement(this);
	});
	
	//------menu---------
	var submenus = $('ul.level2');

	$('ul.top_menu li a').each(function(index){
		var obj = $(this);
		obj.index = index;
        obj.click(function () {
			if (obj.hasClass('active')) return false;

			$('ul.top_menu li.active').removeClass('active');
			obj.parent('li').addClass('active');

			$('ul.level2:visible').hide();
			active_submenu = submenus.get(obj.index);
			$(active_submenu).show();

			return false;
		});
	});

	//------- form tables ----------------
	var formTables = $('table.formTable tr');

	formTables.each(function(){
		formHighLight($(this));
	});
	//------------------------------------	

	$('.editor-init-button').livequery('click', function(){
		return addEditorInstance($(this).attr('for'));
	});
});

var activeTextAreaId;
function addEditorInstance(id)
{	
	if ( CKEDITOR.instances[id] ) 
	{
		CKEDITOR.instances[id].destroy();
		return false;
	}	

	var jObj = $('#'+id);
	if (!jObj) return false;	
	
	var toolbar = 'basic';
	if (jObj.hasClass('toolbar-small')) toolbar = 'small';

	var editor = CKEDITOR.replace( id,
						{
							toolbar : toolbar,
							height: jObj.height()
						});
	editor.on('focus', function(){
		activeTextAreaId = id;
	});
	
	return false;
}

function formHighLight(elem)
{
	elem.mouseover(function(){
		this.style.background = '#eaeaea';
	});
	elem.mouseout(function(){
		this.style.background = 'none';
	});
}

function showMessage(message, is_error, is_static)
{
	params = {};
	if (is_error) params.theme = 'messageError';
	else params.theme = 'messageDone';

	if (is_static) params.sticky = true;

	$.jGrowl(message, params);
}

function confirmDelElement(domElement)
{
	if (confirm($(domElement).attr('title'))) return true;
	return false;
}	

function insertAtCursor(myField, myValue) 
{
	//IE support
	if (document.selection) 
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') 
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	} 
	else 
	{
		myField.value += myValue;
	}
}